<template>
  <div></div>
</template>
<script>
import { qrCodeVerify } from "@/api/public.js";
import { loginByCode } from "@/api/public.js";

export default {
  data() {
    return {
      code: "",
      url: "",
      qrCode: this.$route.query.qrcode,
      fromBy: "",
    };
  },
  beforeCreate() {
    document
      .querySelector("body")
      .setAttribute("style", "background-color:#fff");
  },
  beforeDestroy() {
    document.querySelector("body").setAttribute("style", "");
  },
  created() {
    this.getCode();
  },
  methods: {
    getCode() {
      this.fromBy = this.$route.query.fromBy || this.getUrlCode().fromBy;
      this.code = this.$route.query.code || this.getUrlCode().code; // 截取code
      //如果请求的code是空的，那么先调用认证去获取code
      if (this.code == null || this.code === "") {
        this.callAuth();
      } else {
        //如果不为空，那么code就是我们的openID处理加密后是数据，code需要解密
        this.loginByCode();
      }
    },
    getUrlCode() {
      // 截取url中的code方法
      var url = location.search;
      this.winUrl = url;
      var theRequest = new Object();
      if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        var strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
          theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
        }
      }
      return theRequest;
    },
    callAuth() {
      var local = window.location.href; // 获取页面url\
      var wxUrl = "https://wx.safetyhua.com";
      if(process.env.VUE_APP_WX_URL){
        wxUrl = process.env.VUE_APP_WX_URL
      }
      var newUrl = wxUrl+ "/WxOpen/Index?redirectUrl=" +
        encodeURIComponent(local);
      window.location.href = newUrl;
    },
    loginByCode() {
      //此处code已经是OpenID
      loginByCode({ code: this.code, silence: true })
        .then((res) => {
          if (res.success && res.data.UserName) {
            // 修改登陆状态
            this.$store.commit("updateLogin", true);
            // 把token存入store
            this.$store.commit("updateToken", res.data.UserName);
            // 把用户名存入store
            this.$store.commit("updateUserName", res.data.UserName);
            // 把用户名存入store
            this.$store.commit("updateChnName", res.data.ChnName);
            // 保存地址 updateArea
            this.$store.commit("updateArea", res.data.ApplyArea);
            // 保存地址 updateArea
            this.$store.commit("updateCompanySysType", res.data.CompanySysType);
            // 公司code
            this.$store.commit("updateCompanyCode", res.data.CompanyCode);
            // 模块
          this.$store.commit("setIsTempUser", res.data.IsTempUser);
            this.$store
              .dispatch("getRoles", res.data.UserKind)
              .then((data) => {
                console.log(data);
              })
              .catch((err) => {
                this.$toast(err.message)
              });
            //扫码登陆成功后由主页来进行判断需要进入哪个子界面
            if(this.fromBy && this.fromBy == "follow"){
              this.$router.push({
                path:"/followSuccess",
                query: {
                  code:this.code,
                  success:true,
                  accountCode:res.data.AccountCode,
                },
              })
            }else{
              this.$router.push({
                name: "index",
                query: {
                  num: Math.ceil(Math.random() * 100000),
                },
                params: {
                  qrcode: this.qrCode,
                },
              });
            }
            //代表自动登录失败，需要验证qrCode类型，判断跳转路径
          } else {
            if (this.qrCode) {
              qrCodeVerify({ code: this.qrCode, silence: true }).then((r) => {
                if (r.data.Data) {
                  var qrEntity = JSON.parse(r.data.Data);
                  switch (r.data.CodeType) {
                    case "ConstructSafeProject":
                      this.$router.push({
                        path: "/loginIDCard",
                        query: {
                          CompanyCode: qrEntity.Code,
                        },
                      });
                      break;
                    default:
                      //默认跳转登录页
                      this.$router.push({
                        path: "/login",
                        query:{
                           qrcode: this.qrCode 
                        }
                      });
                      break;
                  }
                }
              });
            } else if(this.fromBy && this.fromBy == "follow"){
              this.$router.push({
                path:"/follow",
                query: {
                    code:this.code
                },
              })
            }else {
              this.$router.push({
                path: "/login",
              });
            }
          }
        })
        .catch((err) => {
          this.$toast(err.message);
        });
    },
  },
};
</script>